package com.job.servicejob.controller;


import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.job.commonutils.R;
import com.job.servicejob.entity.Student;
import com.job.servicejob.entity.vo.StudentQuery;
import com.job.servicejob.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author myt
 * @since 2023-03-20
 */
@RestController
@RequestMapping("/servicejob/student")
@CrossOrigin
public class StudentController {

    @Autowired
    private StudentService studentService;

     //查询所有
    @GetMapping("findAllStudent")

    public R findAllStudent(){
        List<Student> list = studentService.list(null);
         return R.ok().data("item",list);

    }
    //根据手机号查询
    @GetMapping("getInfo/{phone}")
    public  R getInfo(@PathVariable String phone){
//    Admin stu = adminService.getById(phone);
        List<Student> stu = studentService.getByPhone(phone);
        return R.ok().data("stu",stu);
    }
    // //逻辑删除job
    //    @DeleteMapping("{id}")
    //    //@PathVariable String id得到路径中的id值
    //    public R removeJob(@PathVariable Integer id){
    //        boolean flag = jobService.removeById(id);
    //        if (flag){
    //            return R.ok();
    //        }else {
    //            return R.error();
    //        }
    //    }
    @DeleteMapping("{id}")
    public R removeJob(@PathVariable Integer id){
                boolean flag = studentService.removeById(id);
                if (flag){
                    return R.ok();
                }else {
                    return R.error();
                }
            }

    //添加学生

    @PostMapping("addStudent")
    public R addStudent(@RequestBody Student student ){
        boolean save = studentService.save(student);
        if(save){
            return R.ok();
        }else {
            return R.error();
        }
    }


    //分页
    @GetMapping("pageStudent/{current}/{limit}")
    public R pageListJob(@PathVariable long current,
                         @PathVariable long limit){
        //创建配置对象
        Page<Student> pageStudent = new Page<>(current,limit);
        //调用方法实现分页
        //调用方法时，底层封装，把分页所有数据封装到pageJob对象里面
        studentService.page(pageStudent,null);

        long total = pageStudent.getTotal();//总记录数
        List<Student> records = pageStudent.getRecords();//数据list集合

//        使用map返回结果
//        Map map = new HashMap();
//        map.put("total",total);
//        map.put("rows",records);
//        return R.ok().data(map);

        return R.ok().data("total",total).data("rows",records);
    }

    // //4条件查询带分页
        @PostMapping("pageStudentCondition/{current}/{limit}")
        public R pageStudentCondition(@PathVariable long current,@PathVariable long limit,
                                  @RequestBody(required = false) StudentQuery studentQuery){
            //创建page对象
            Page<Student> pageStudent = new Page<>(current,limit);
            //构建条件
            QueryWrapper<Student> wrapper = new QueryWrapper<>();
            //多条件组合查询
            //动态sql
            //判断条件值是否为空，如果不空 拼接条件
            String name = studentQuery.getName();
            String qingongjianxue_tatus = studentQuery.getQingongjianxue_tatus();

            if(!StringUtils.isEmpty(name)){
                //构建条件
                wrapper.like("name",name);
            }
            if(!StringUtils.isEmpty(qingongjianxue_tatus)){
                //构建条件
                wrapper.like("qingongjianxue_tatus",qingongjianxue_tatus);
            }

            //调用方法实现分页查询
            studentService.page(pageStudent,wrapper);
            long total = pageStudent.getTotal();//总记录数
            List<Student> records = pageStudent.getRecords();//数据list集合
            return R.ok().data("total",total).data("rows",records);
        }

    //修改操作
    //1.根据id查询岗位
    @GetMapping("getStudent/{id}")
    public R getStudent(@PathVariable String id){
        Student student = studentService.getById(id);
        return R.ok().data("student",student);

    }
    //2.修改
    @PostMapping("updateStudent")
    public R updateStudent (@RequestBody Student student){
        boolean flag = studentService.updateById(student);
        if(flag){
            return R.ok();
        }else {
            return R.error();
        }
    }

    //查询各个学院人数
//    @GetMapping ("countCollege/{college}")
//    public R countCollege(@PathVariable String college){
//      Integer count =  studentService.countCollegeNum(college);
//      return R.ok().data("countCollege",count);
//    }

    @GetMapping("/members")
    public R members() {
        List<Student> list = studentService.list(null);
        int college1 = 0; //信工学院
        int college2 = 0; //金融贸易学院
        int college3 = 0; //传媒与艺术学院
        int college4 = 0; //国际学院
        for (Student student: list) {
            String college = student.getCollege();
            switch (college) {
                case "信息工程学院": college1 += 1; break;
                case "金融贸易学院": college2 += 1; break;
                case "传媒与艺术学院": college3 += 1; break;
                case "国际学院": college4 += 1; break;
                default: break;
            }
        }
        return R.ok().data("members",CollUtil.newArrayList(college1,college2,college3,college4));
    }


}

